package org.apache.ibatis.demo.handler;

import java.math.BigDecimal;
import java.text.DecimalFormat;

/**
 * MyFloat
 *
 * @author zhaohui
 * @date 2019/12/24 11:39
 */
public class MyFloat extends Number {

    private int scale;

    private BigDecimal value;

    private BigDecimal DEFAULT_VALUE;

    {
        DEFAULT_VALUE = new BigDecimal(0);
        DEFAULT_VALUE.setScale(6);
        scale = 6;
    }

    public MyFloat() {
        this(new BigDecimal(0), 6);
    }

    @Override
    public int intValue() {
        return value.intValue();
    }

    @Override
    public long longValue() {
        return value.longValue();
    }

    @Override
    public float floatValue() {
        return value.floatValue();
    }

    @Override
    public double doubleValue() {
        return value.doubleValue();
    }

    public MyFloat(int value) {
        this(new BigDecimal(value));
    }

    public MyFloat(double value) {
        this(new BigDecimal(value));
    }

    public MyFloat(BigDecimal value) {
        this(value, 6);
    }

    public MyFloat(BigDecimal value, int scale) {
        this.value = null == value ? new BigDecimal(0) : value;
        this.value.setScale(scale, BigDecimal.ROUND_HALF_DOWN);
    }

    public MyFloat add(MyFloat num) {
        return new MyFloat(value.add(num.value));
    }

    public BigDecimal getValue() {
        return null == value ? DEFAULT_VALUE : value;
    }

    @Override
    public String toString() {
        return new DecimalFormat("0.000000").format(value);
    }

    public static void main(String[] args) {
        MyFloat add = new MyFloat(10.123D).add(new MyFloat(10));
        System.out.println(add);
    }
}
